package com.bionic.banking.commands;

import com.bionic.banking.auth.manager.Config;
import com.bionic.banking.auth.manager.Message;
import com.bionic.banking.core.enums.BankTransactionTypeEnum;
import com.bionic.banking.dao.connection.LoggerLoader;
import com.bionic.banking.logic.controller.BankTransactionController;
import com.bionic.banking.logic.controller.BankTransactionTypeController;
import com.bionic.banking.logic.exception.*;
import java.math.BigInteger;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class CommandCreateBankOperation implements ICommand {

    private static final org.apache.log4j.Logger logger =
            LoggerLoader.getLogger(CommandCreateBankOperation.class);

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        String page = null;

        //1. read input request parameters
        String description = (String) request.getParameter("description");
        Integer baseAccountId = new Integer(request.getParameter("baseAccountId"));        

        BigInteger balance = new BigInteger(request.getParameter("balance"));
        BigInteger amount = new BigInteger(request.getParameter("amount"));

        String currency = (String) request.getParameter("currency");

        try {
            if (currency == null) {
                throw new CurrencyNotFoundException("CurrencyCode is not passed as parameter");
            }
            Integer currencyCode = Integer.valueOf(currency);

            String txTypeName = (String) request.getParameter("transactionType");          
            Integer targetAccountId =
                (BankTransactionTypeEnum.parseString(txTypeName) == BankTransactionTypeEnum.TransferFunds) ?
                 new Integer(request.getParameter("targetAccountId")): 0; 

            BankTransactionTypeController txTypeBean = new BankTransactionTypeController();
            Integer txTypeId = txTypeBean.getByName(txTypeName).getTxTypeId();

            //2. Use session bean to commit newly created entity into database
            BankTransactionController bean = null;

            try {
                bean = new BankTransactionController();
                bean.createTransaction(txTypeName, amount, currencyCode, description,
                        baseAccountId, targetAccountId);
                //page = Config.getInstance().getProperty(Config.BANKTRANSACTIONLIST);
                request.setAttribute("content", Config.getInstance().getProperty(Config.BANKTRANSACTIONLIST));
                page = Config.getInstance().getProperty(Config.MAIN);

            } catch (AccountNotFoundException ex) {
                logger.error(ex);
                request.setAttribute("error", ex.getMessage()
                        + " in " + CommandCreateBankOperation.class);
                page = Config.getInstance().getProperty(Config.ERROR);


            } catch (InsufficientCreditException | InsufficientFundsException ex) {
                logger.error(ex);
                request.setAttribute("error",
                        Message.getInstance().getProperty(Message.BANKTRANSACTION_INSUFFICIENTFUNDS_ERROR)
                        + " in " + CommandCreateBankOperation.class);
                page = Config.getInstance().getProperty(Config.ERROR);
            }
        } catch (InvalidParameterException | IllegalAccountTypeException |
                InstantiationException | IllegalAccessException ex) {
            logger.error(ex);
            request.setAttribute("error", ex.getMessage()
                    + " in " + CommandCreateBankOperation.class);
            page = Config.getInstance().getProperty(Config.ERROR);
        } catch (CurrencyNotFoundException ex) {
            logger.error(ex);
            request.setAttribute("error",
                    Message.getInstance().getProperty(Message.CURRENCY_NOTFOUND_ERROR)
                    + " in " + CommandCreateBankOperation.class);
            page = Config.getInstance().getProperty(Config.ERROR);

        } catch (TransactionTypeNotFoundException ex) {
            logger.error(ex);
            request.setAttribute("error",
                    Message.getInstance().getProperty(Message.TRANSACTIONTYPE_NOTFOUND_ERROR)
                    + " in " + CommandCreateBankOperation.class);
            page = Config.getInstance().getProperty(Config.ERROR);
        } catch (TransactionFailedException ex) {
            logger.error(ex);
            request.setAttribute("error",
                    Message.getInstance().getProperty(Message.CREATE_BANKTRANSACTION_ERROR)
                    + " in " + CommandCreateBankOperation.class);
            page = Config.getInstance().getProperty(Config.ERROR);

        }
        return page;
    }
}
